package com.example.imagesteganographylibrary.Text;


import com.example.imagesteganographylibrary.Utils.Crypto;
import com.example.imagesteganographylibrary.Utils.Utility;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;


/**
 * This main class of the text steganography
 */
public class ImageSteganography {

    private String message;
    private String secret_key;
    private String encrypted_message;
    private PixelMap image;
    private PixelMap encoded_image;
    private Boolean encoded;
    private Boolean decoded;
    private Boolean secretKeyWrong;

    public ImageSteganography() {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = PixelFormat.ARGB_8888;
        options.size = new Size(600, 600);
        this.encoded = false;
        this.decoded = false;
        this.secretKeyWrong = true;
        this.message = "";
        this.secret_key = "";
        this.encrypted_message = "";
        this.image = PixelMap.create(options);
        this.encoded_image = PixelMap.create(options);
    }

    public ImageSteganography(String message, String secret_key, PixelMap image) {
        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
        opts.pixelFormat = PixelFormat.ARGB_8888;
        opts.size = new Size(600, 600);
        this.message = message;
        this.secret_key = convertKeyTo128bit(secret_key);
        this.image = image;
        this.encrypted_message = encryptMessage(message, this.secret_key);
        this.encoded = false;
        this.decoded = false;
        this.secretKeyWrong = true;

        this.encoded_image = PixelMap.create(opts);

    }

    public ImageSteganography(String secret_key, PixelMap image) {

        PixelMap.InitializationOptions opts1 = new PixelMap.InitializationOptions();
        opts1.pixelFormat = PixelFormat.ARGB_8888;
        opts1.size = new Size(600, 600);
        this.secret_key = convertKeyTo128bit(secret_key);
        this.image = image;

        this.encoded = false;
        this.decoded = false;
        this.secretKeyWrong = true;

        this.message = "";
        this.encrypted_message = "";
        this.encoded_image = PixelMap.create(opts1);
    }

    private static String encryptMessage(String message, String secret_key) {
        String encrypted_message = "";
        if (message != null) {
            if (!Utility.isStringEmpty(secret_key)) {
                try {
                    encrypted_message = Crypto.encryptMessage(message, secret_key);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                encrypted_message = message;
            }
        }
        return encrypted_message;
    }

    public static String decryptMessage(String message, String secret_key) {
        String decrypted_message = "";
        if (message != null) {
            if (!Utility.isStringEmpty(secret_key)) {
                try {
                    decrypted_message = Crypto.decryptMessage(message, secret_key);
                } catch (Exception e) {
//                    Log.d(TAG, "Error : " + e.getMessage() + " , may be due to wrong key.");
                }
            } else {
                decrypted_message = message;
            }
        }

        return decrypted_message;
    }

    private static String convertKeyTo128bit(String secret_key) {

        StringBuilder result = new StringBuilder(secret_key);

        if (secret_key.length() <= 16) {
            for (int i = 0; i < (16 - secret_key.length()); i++) {
                result.append("#");
            }
        } else {
            result = new StringBuilder(result.substring(0, 15));
        }

        return result.toString();
    }

    public PixelMap getEncoded_image() {
        return encoded_image;
    }

    public void setEncoded_image(PixelMap encoded_image) {

        this.encoded_image = encoded_image;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSecret_key() {
        return secret_key;
    }

    public void setSecret_key(String secret_key) {
        this.secret_key = secret_key;
    }

    public String getEncrypted_message() {
        return encrypted_message;
    }

    public void setEncrypted_message(String encrypted_message) {
        this.encrypted_message = encrypted_message;
    }

    public PixelMap getImage() {
        return image;
    }

    public void setImage(PixelMap image) {
        this.image = image;
    }

    public Boolean isEncoded() {
        return encoded;
    }

    public void setEncoded(Boolean encoded) {
        this.encoded = encoded;
    }

    public Boolean isDecoded() {
        return decoded;
    }

    public void setDecoded(Boolean decoded) {
        this.decoded = decoded;
    }

    public Boolean isSecretKeyWrong() {
        return secretKeyWrong;
    }

    public void setSecretKeyWrong(Boolean secretKeyWrong) {
        this.secretKeyWrong = secretKeyWrong;
    }
}
